<template>
	<view style="height:100vh;background: #fff;">
		<view class="img-a">
			<view class="t-b">
				您好，
				<br />
				欢迎
			</view>
		</view>
		<view class="loging-view" style="">
			<view class="t-login">
				<!-- 新增：用户名密码登录表单 -->
				<view class="login-form">
					<view class="input-group">
						<input class="login-input" v-model="username" placeholder="请输入用户名"
							placeholder-style="color:#999;" />
					</view>
					<view class="input-group">
						<input class="login-input" v-model="password" placeholder="请输入密码"
							placeholder-style="color:#999;" password type="password" />
					</view>
					<button class="login-button" @click="loginByAccount()">账号密码登录</button>
				</view>

				<view class="login-main">
					<button class="login-button" @click="loginByWx()">微信一键登录</button>
				</view>
				<view class="authority-radio">
					<radio @click="changeAuthority()" :checked="hasAuthorized " style="transform:scale(0.7)" />同意并接受
					<text @click="goToUserAgreement" class="blue-text">《用户服务协议》</text>
					和
					<text @click="goToPrivacyPolicy" class="blue-text">《隐私政策》</text>
				</view>
				<view class="t-f"><text>————其他登录方式————</text></view>
				<view class="t-e ">
					<image class="t-g" src="../../static/手机号登录.png" @tap="toLoginByPhone()"></image>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		jwtDecode
	} from "jwt-decode";
	export default {
		data() {
			return {
				token: '',
				hasAuthorized: null,
				UserCode: '',
				userInfo: {
					userId: 0,
					userName: "",
					sex: "",
					phone: "",
					status: "",
					avatar: ""
				},
				username: '本',
				password: '123456'
			}
		},
		onLoad() {
			this.hasAuthorized = uni.getStorageSync('hasAuthorized')
		},
		methods: {
			goToUserAgreement() {
				uni.navigateTo({
					url: "/page_user/user/UserAgreement"
				})
			},
			goToPrivacyPolicy() {
				uni.navigateTo({
					url: "/page_user/user/PrivacyPolicy"
				})
			},
			// 账号密码登录
			loginByAccount() {
				if (!this.username || !this.password) {
					uni.showToast({
						title: '请输入用户名和密码',
						icon: 'none'
					});
					return;
				}

				uni.showLoading({
					title: '登录中...'
				});

				uni.request({
					url: this.$baseUrl + '/user/login',
					method: 'POST',
					data: {
						username: this.username,
						password: this.password
					},
					success: (res) => {
						uni.hideLoading();
						console.log(res.data)
						if (res.data.success === true) {
							uni.setStorageSync('token', res.data.data.accessToken)
							console.log('存储的token为' + uni.getStorageSync('token'))
							
							uni.showToast({
								title: '登录成功'
							});
							console.log("登录成功!!!")
							// 登录成功处理，例如跳转页面
							uni.navigateTo({
								url: "/pages/index/index"
							});
						} else {
							uni.showToast({
								title: '登录失败',
								icon: 'none'
							});
						}
					},
					fail: (err) => {
						uni.hideLoading();
						uni.showToast({
							title: '网络错误，请重试',
							icon: 'none'
						});
						console.error('登录请求失败:', err);
					}
				});
			},


			loginByWx: function() {

				if (this.hasAuthorized == false || this.hasAuthorized == null) {
					uni.showModal({
						content: '请阅读并勾选同意用户隐私政策 ',
					})
				} else {
					wx.login({
						success: (res) => {
							console.log(res, 'wx.login()返回的数据');
							this.UserCode = res.code;
							wx.request({
								url: this.$baseUrl + '/user/loginByWx',
								header: {
									'Content-Type': 'application/x-www-form-urlencoded',
								},
								method: 'POST',
								data: {
									code: this.UserCode,
								},
								dataType: 'json',
								success: (res) => {
									console.log(this.UserCode, '发送请求时的code');
									console.log(res, '登录后返回的数据111');
									const response = res.data
									if (response.code == 200) {
										uni.navigateTo({
											url: '/pages/index/index',
											success() {
												const phone = response.data.userInfo
													.phone
												//手机号不为空，直接登录
												//保存用户状态信息
												uni.setStorageSync('userInfo', response
													.data.userInfo)
												// uni.setStorageSync('token',response.data.token)
												uni.setStorageSync('token', res.data
													.data.token)
												console.log('code为' + res.data.code)
												console.log('mes为' + res.data.message)
												console.log('token为' + res.data.data
													.token);

												console.log('存储的token为' + uni
													.getStorageSync('token'))
												console.log('用户id为' + res.data.data
													.userInfo.userId);
												uni.setStorageSync('userId', res.data
													.data.userInfo.userId)
												const decoded = jwtDecode(res.data.data
													.token);
												console.log('解析后的jwt为' + decoded
													.loginId);
											}
										})
									} else {
										if (response.message == '用户已登录') {
											console.log('存储的用户基本信息：' + uni.getStorageSync(
												'userInfo').userId);
											uni.showModal({
												title: '提示',
												content: response.message,
												showCancel: false,
												success() {
													uni.navigateTo({
														url: "/pages/index/index"
													})
												}
											})

										} else {
											console.log('登陆失败：{}', response.message);
											uni.showModal({
												title: '提示',
												content: '登录失败：' + response.message,
												showCancel: false,
												success() {
													// 跳转到登录页
													console.log('弹窗success');
													uni.navigateTo({
														url: "/page_user/user/LoginByPhone"
													})
												}
											})
										}

									}
								},
							})
						}
					})
				}

			},
			changeAuthority: function(e) {
				this.hasAuthorized = !this.hasAuthorized;
				uni.setStorageSync('hasAuthorized', this.hasAuthorized);
			},
			toLoginByPhone: function() {
				uni.navigateTo({
					url: '/page_user/user/LoginByPhone'
				})
			},
			logout: function() {
				wx.request({
					url: this.$baseUrl + '/user/logout',
					//后续需要携带请求头token
					header: {
						token: uni.getStorageSync('token')
					},
					method: 'POST',
					success: (res) => {
						const response = res.data
						if (response.code == 200) {
							try {
								uni.clearStorageSync();
								uni.navigateTo({
									url: '/pages/index/index'
								})
							} catch (e) {
								// error
								uni.showToast({
									title: '退出失败'
								})
							}
						} else {
							uin.showModal({
								content: response.message
							})
						}
					},
				})
			},
		}
	}
</script>

<style>
	.img-a {
		width: 100%;
		height: 550rpx;
		/* height: 50vh; */
		background-image: url(https://zhoukaiwen.com/img/loginImg/head.png);
		/* background-image: url(/static/loginBackground.png); */
		background-size: 100%;
	}

	.t-b {
		text-align: left;
		font-size: 42rpx;
		color: #ffffff;
		padding: 130rpx 0 0 70rpx;
		font-weight: bold;
		line-height: 70rpx;
	}

	.loging-view {
		width: 100%;
		/* height: 80%; */
		height: 775rpx;
		position: relative;
		margin-top: -120rpx;
		background-color: #ffffff;
		border-radius: 8% 8% 0% 0;
	}

	.login-button {
		font-size: 28rpx;
		color: #fff;
		height: 90rpx;
		line-height: 90rpx;
		border-radius: 50rpx;
		font-weight: bold;
		background: #f5f6fa;
		color: #000000;
		text-align: center;
		margin-top: 10rpx;
	}

	.t-login {
		width: 600rpx;
		margin: 0 auto;
		font-size: 28rpx;
		padding-top: 150rpx;
		padding-bottom: 150rpx
	}

	.t-login button {
		font-size: 28rpx;
		background: #2796f2;
		color: #fff;
		height: 90rpx;
		line-height: 90rpx;
		border-radius: 50rpx;
		font-weight: bold;
	}

	.t-login .t-e {
		margin-top: 50rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		/* 	text-align: center;
	width: 250rpx;	
	/* margin-top: 50rpx; */
		margin: 40rpx auto 0;
	}

	.t-login .t-e image {
		width: 50rpx;
		height: 50rpx;
	}

	.t-login .t-f {
		text-align: center;
		margin-top: 100rpx;
		/* margin: 150rpx 0 0 0; */
		color: #666;
	}

	.t-login .t-f text {
		/* margin-top: 10rpx; */
		/* margin-left: 20rpx; */
		color: #aaaaaa;
		font-size: 27rpx;
	}

	.t-login .t-g {
		display: flex;
		justify-content: center;
		align-items: center;
		/* float: left; */
		width: 50%;
	}

	/* .cl{
	margin-top: 100rpx;
} */
	/* .cl {
	zoom: 1;
} */


	.cl:after {
		clear: both;
		display: block;
		visibility: hidden;
		height: 0;
		content: '\20';
	}


	.authority-radio {

		margin-top: 50rpx;
	}

	.blue-text {
		color: blue;
	}

	.login-form {
		margin-bottom: 40rpx;
	}

	.input-group {
		margin-bottom: 30rpx;
		border-bottom: 1px solid #eee;
		padding-bottom: 10rpx;
	}

	.login-input {
		height: 80rpx;
		font-size: 32rpx;
		padding: 0 20rpx;
	}

	.login-button {
		background-color: #007AFF;
		color: #fff;
		margin-top: 40rpx;
		border-radius: 50rpx;
	}

	.blue-text {
		color: #007AFF;
	}
</style>